package mediathek.gui.dialogEinstellungen;

import com.formdev.flatlaf.util.ScaledImageIcon;
import mediathek.config.Daten;
import mediathek.config.Konstanten;
import mediathek.daten.Country;
import mediathek.gui.dialog.DialogHilfe;
import mediathek.gui.messages.BlacklistChangedEvent;
import mediathek.gui.messages.GeoStateChangedEvent;
import mediathek.tool.*;
import net.miginfocom.layout.AC;
import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;
import org.jdesktop.swingx.VerticalLayout;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Objects;

public class PanelEinstellungenGeo extends JPanel {
    private final JFrame parentComponent;

    public PanelEinstellungenGeo(JFrame pparentComponent) {
        parentComponent = pparentComponent;

        initComponents();
        setCountryFlags();
        init();
    }

    private void setCountryFlags() {
        lblIcon_DE.setIcon(getScaledIconResource("/icons/countries/162-germany.png"));
        lblIcon_AT.setIcon(getScaledIconResource("/icons/countries/003-austria.png"));
        lblIcon_CH.setIcon(getScaledIconResource("/icons/countries/205-switzerland.png"));
        lblIcon_FR.setIcon(getScaledIconResource("/icons/countries/195-france.png"));
        lblIcon_EU.setIcon(getScaledIconResource("/icons/countries/259-european-union.png"));
    }

    private static final Dimension FLAG_DIMENSIONS = new Dimension(32,24);

    private ScaledImageIcon getScaledIconResource(@NotNull String url) {
        var icon = new ImageIcon(Objects.requireNonNull(getClass().getResource(url)));
        var imageDim = new Dimension(icon.getIconWidth(), icon.getIconHeight());
        var destDim = GuiFunktionen.calculateFittedDimension(imageDim, FLAG_DIMENSIONS);
        return new ScaledImageIcon(icon, destDim.width, destDim.height);
    }

    private void init() {
        switch (ApplicationConfiguration.getInstance().getGeographicLocation()) {
            case CH -> jRadioButtonCH.setSelected(true);
            case AT -> jRadioButtonAt.setSelected(true);
            case EU -> jRadioButtonEu.setSelected(true);
            case FR -> radioButtonFR.setSelected(true);
            case OTHER -> jRadioButtonSonst.setSelected(true);
            default -> jRadioButtonDe.setSelected(true);
        }
        jRadioButtonDe.addActionListener(_ -> {
            ApplicationConfiguration.getInstance().setGeographicLocation(Country.DE);
            filterBlacklistAndNotifyChanges();
        });
        radioButtonFR.addActionListener(_ -> {
            ApplicationConfiguration.getInstance().setGeographicLocation(Country.FR);
            filterBlacklistAndNotifyChanges();
        });
        jRadioButtonCH.addActionListener(_ -> {
            ApplicationConfiguration.getInstance().setGeographicLocation(Country.CH);
            filterBlacklistAndNotifyChanges();
        });
        jRadioButtonAt.addActionListener(_ -> {
            ApplicationConfiguration.getInstance().setGeographicLocation(Country.AT);
            filterBlacklistAndNotifyChanges();
        });
        jRadioButtonEu.addActionListener(_ -> {
            ApplicationConfiguration.getInstance().setGeographicLocation(Country.EU);
            filterBlacklistAndNotifyChanges();
        });
        jRadioButtonSonst.addActionListener(_ -> {
            ApplicationConfiguration.getInstance().setGeographicLocation(Country.OTHER);
            filterBlacklistAndNotifyChanges();
        });

        jButtonHilfe.setIcon(SVGIconUtilities.createSVGIcon("icons/fontawesome/circle-question.svg"));
        jButtonHilfe.addActionListener(_ -> new DialogHilfe(parentComponent, true, new GetFile().getHilfeSuchen(Konstanten.PFAD_HILFETEXT_GEO)).setVisible(true));
    }

    /**
     * Filter blacklist and notify other components that changes were made.
     */
    private void filterBlacklistAndNotifyChanges() {
        final var daten = Daten.getInstance();

        daten.getListeBlacklist().filterListe();
        MessageBus.getMessageBus().publishAsync(new GeoStateChangedEvent());
        MessageBus.getMessageBus().publishAsync(new BlacklistChangedEvent());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    // Generated using JFormDesigner non-commercial license
    private void initComponents() {
        var panel1 = new JPanel();
        var panel2 = new JPanel();
        jRadioButtonDe = new JRadioButton();
        lblIcon_DE = new JLabel();
        var label2 = new JLabel();
        var panel3 = new JPanel();
        jRadioButtonAt = new JRadioButton();
        lblIcon_AT = new JLabel();
        var label4 = new JLabel();
        var panel4 = new JPanel();
        jRadioButtonCH = new JRadioButton();
        lblIcon_CH = new JLabel();
        var label6 = new JLabel();
        var panel5 = new JPanel();
        radioButtonFR = new JRadioButton();
        lblIcon_FR = new JLabel();
        var label8 = new JLabel();
        var panel6 = new JPanel();
        jRadioButtonEu = new JRadioButton();
        lblIcon_EU = new JLabel();
        var label10 = new JLabel();
        jRadioButtonSonst = new JRadioButton();
        jButtonHilfe = new JButton();

        //======== this ========
        setLayout(new MigLayout(
            new LC().insets("5").hideMode(3).gridGap("5", "5"), //NON-NLS
            // columns
            new AC()
                .grow().fill(),
            // rows
            new AC()
                .gap()
                .fill()));

        //======== panel1 ========
        {
            panel1.setBorder(new TitledBorder("Mein Standort")); //NON-NLS
            panel1.setLayout(new VerticalLayout());

            //======== panel2 ========
            {
                panel2.setLayout(new MigLayout(
                    new LC().insets("0").hideMode(3), //NON-NLS
                    // columns
                    new AC()
                        .grow().align("left"), //NON-NLS
                    // rows
                    new AC()
                        .fill()));

                //---- jRadioButtonDe ----
                jRadioButtonDe.setSelected(true);
                panel2.add(jRadioButtonDe, new CC().cell(0, 0));
                panel2.add(lblIcon_DE, new CC().cell(0, 0));

                //---- label2 ----
                label2.setText("Deutschland (DE)"); //NON-NLS
                panel2.add(label2, new CC().cell(0, 0));
            }
            panel1.add(panel2);

            //======== panel3 ========
            {
                panel3.setLayout(new MigLayout(
                    new LC().insets("0").hideMode(3), //NON-NLS
                    // columns
                    new AC()
                        .grow().align("left"), //NON-NLS
                    // rows
                    new AC()
                        .fill()));
                panel3.add(jRadioButtonAt, new CC().cell(0, 0));
                panel3.add(lblIcon_AT, new CC().cell(0, 0));

                //---- label4 ----
                label4.setText("\u00d6sterreich (AT)"); //NON-NLS
                panel3.add(label4, new CC().cell(0, 0));
            }
            panel1.add(panel3);

            //======== panel4 ========
            {
                panel4.setLayout(new MigLayout(
                    new LC().insets("0").hideMode(3), //NON-NLS
                    // columns
                    new AC()
                        .grow().align("left"), //NON-NLS
                    // rows
                    new AC()
                        .fill()));
                panel4.add(jRadioButtonCH, new CC().cell(0, 0));
                panel4.add(lblIcon_CH, new CC().cell(0, 0));

                //---- label6 ----
                label6.setText("Schweiz (CH)"); //NON-NLS
                panel4.add(label6, new CC().cell(0, 0));
            }
            panel1.add(panel4);

            //======== panel5 ========
            {
                panel5.setLayout(new MigLayout(
                    new LC().insets("0").hideMode(3), //NON-NLS
                    // columns
                    new AC()
                        .grow().align("left"), //NON-NLS
                    // rows
                    new AC()
                        .fill()));
                panel5.add(radioButtonFR, new CC().cell(0, 0));
                panel5.add(lblIcon_FR, new CC().cell(0, 0));

                //---- label8 ----
                label8.setText("Frankreich (FR)"); //NON-NLS
                panel5.add(label8, new CC().cell(0, 0));
            }
            panel1.add(panel5);

            //======== panel6 ========
            {
                panel6.setLayout(new MigLayout(
                    new LC().insets("0").hideMode(3), //NON-NLS
                    // columns
                    new AC()
                        .grow().align("left"), //NON-NLS
                    // rows
                    new AC()
                        .fill()));
                panel6.add(jRadioButtonEu, new CC().cell(0, 0));
                panel6.add(lblIcon_EU, new CC().cell(0, 0));

                //---- label10 ----
                label10.setText("EU (EBU - European Broadcasting Union)"); //NON-NLS
                panel6.add(label10, new CC().cell(0, 0));
            }
            panel1.add(panel6);

            //---- jRadioButtonSonst ----
            jRadioButtonSonst.setText("Sonstiger"); //NON-NLS
            panel1.add(jRadioButtonSonst);
        }
        add(panel1, new CC().cell(0, 0));

        //---- jButtonHilfe ----
        jButtonHilfe.setIcon(null);
        jButtonHilfe.setToolTipText("Hilfe anzeigen"); //NON-NLS
        add(jButtonHilfe, new CC().cell(0, 1).alignX("right").growX(0)); //NON-NLS

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(jRadioButtonDe);
        buttonGroup1.add(jRadioButtonAt);
        buttonGroup1.add(jRadioButtonCH);
        buttonGroup1.add(radioButtonFR);
        buttonGroup1.add(jRadioButtonEu);
        buttonGroup1.add(jRadioButtonSonst);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // Generated using JFormDesigner non-commercial license
    private JRadioButton jRadioButtonDe;
    private JLabel lblIcon_DE;
    private JRadioButton jRadioButtonAt;
    private JLabel lblIcon_AT;
    private JRadioButton jRadioButtonCH;
    private JLabel lblIcon_CH;
    private JRadioButton radioButtonFR;
    private JLabel lblIcon_FR;
    private JRadioButton jRadioButtonEu;
    private JLabel lblIcon_EU;
    private JRadioButton jRadioButtonSonst;
    private JButton jButtonHilfe;
    // End of variables declaration//GEN-END:variables

}
